package com.tkjt.pms.diff.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jeecg.modules.system.entity.SysDataLog;
import org.jeecg.modules.system.mapper.SysDataLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tkjt.pms.common.entity.TkIndexInfo;
import com.tkjt.pms.common.entity.TkIndexValue;
import com.tkjt.pms.common.utils.EsJsonUtils;
import com.tkjt.pms.diff.ITkIndexValueDiffService;
import com.tkjt.pms.modules.common.mapper.TkIndexInfoMapper;
import com.tkjt.pms.modules.common.mapper.TkIndexValueMapper;
import com.tkjt.pms.persist.entity.TkIndexValueDiff;
import com.tkjt.pms.persist.entity.TkIndexValueDiffBaseLine;
import com.tkjt.pms.persist.mapper.TkIndexValueDiffBaseLineMapper;
import com.tkjt.pms.persist.mapper.TkIndexValueDiffMapper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class TkIndexValueDiffService implements ITkIndexValueDiffService {

	@Autowired
	protected TkIndexInfoMapper tkIndexInfoMapper;
	@Autowired
	protected TkIndexValueMapper tkIndexValueMapper;
	@Autowired
	protected TkIndexValueDiffMapper tkIndexValueDiffMapper;
	@Autowired
	protected TkIndexValueDiffBaseLineMapper tkIndexValueDiffBaseLineMapper;
	@Autowired
	protected SysDataLogMapper sysDataLogMapper;

	@Override
	public void doDiff(String dataType, String indexGroup) {

		log.info("clear all data logs, start...");
		this.clearLastDataLogs(dataType, indexGroup);
		log.info("clear all data logs, end.");

		log.info("start process diff, dataType={}, indexGroup={}", dataType, indexGroup);
		this.processIndexGroupDiff(dataType, indexGroup);
		log.info("process diff, end.");
	}

	private void clearLastDataLogs(String dataType, String indexGroup) {		
		log.info("delete all diff, start......");
		if("C".equals(indexGroup)) {
			QueryWrapper<SysDataLog> wrapperDataLogs = new QueryWrapper<SysDataLog>();
			wrapperDataLogs.lambda().eq(SysDataLog::getDataTable, "tk_index_value");
			wrapperDataLogs.lambda().likeLeft(SysDataLog::getDataId, dataType+"@KC");
			sysDataLogMapper.delete(wrapperDataLogs);
			
			QueryWrapper<TkIndexValueDiff> wrapperDiff = new QueryWrapper<TkIndexValueDiff>();
			wrapperDiff.lambda().likeLeft(TkIndexValueDiff::getIndexValueCode, "KC");
			tkIndexValueDiffMapper.delete(wrapperDiff);
		} else if("G".equals(indexGroup)) {
			QueryWrapper<SysDataLog> wrapperDataLogs = new QueryWrapper<SysDataLog>();
			wrapperDataLogs.lambda().eq(SysDataLog::getDataTable, "tk_index_value");
			wrapperDataLogs.lambda().likeLeft(SysDataLog::getDataId, dataType+"@KG");
			sysDataLogMapper.delete(wrapperDataLogs);
			
			QueryWrapper<TkIndexValueDiff> wrapperDiff = new QueryWrapper<TkIndexValueDiff>();
			wrapperDiff.lambda().likeLeft(TkIndexValueDiff::getIndexValueCode, "KG");
			tkIndexValueDiffMapper.delete(wrapperDiff);
		}
		log.info("delete all diff, end.");
	}

	private void processIndexGroupDiff(String dataType, String indexGroup) {

		Map<String, Object> param = new HashMap<String, Object>();
		param.put("indexGroup", indexGroup);
		List<TkIndexInfo> indexsList = tkIndexInfoMapper.selectByArrtsParam(param);

		log.info("process diff, dataType={},indexGroup={}", dataType, indexGroup);
		log.info("process diff, size={}", indexsList.size());
		for (TkIndexInfo indexInfo : indexsList) {
			//log.info("update diff, indexValueCode={}", indexInfo.getIndexValueCode());
			QueryWrapper<TkIndexValue> queryWrapper = new QueryWrapper<TkIndexValue>();
			queryWrapper.lambda().eq(TkIndexValue::getDataType, dataType);
			queryWrapper.lambda().eq(TkIndexValue::getIndexValueCode, indexInfo.getIndexValueCode());
			List<TkIndexValue> indexValues = tkIndexValueMapper.selectList(queryWrapper);
			for (TkIndexValue indexValue : indexValues) {
				
				//重点工作不写入数据对比表中
				if("KCIAAC002VA001".equals(indexValue.getIndexValueCode()) || "KGIAAC002VA001".equals(indexValue.getIndexValueCode())) {
					continue;
				}
				
				QueryWrapper<TkIndexValueDiffBaseLine> blQueryWrapper = new QueryWrapper<TkIndexValueDiffBaseLine>();
				blQueryWrapper.lambda().eq(TkIndexValueDiffBaseLine::getIndexValueCode, indexValue.getIndexValueCode());
				blQueryWrapper.lambda().eq(TkIndexValueDiffBaseLine::getDataType, indexValue.getDataType());
				blQueryWrapper.lambda().eq(TkIndexValueDiffBaseLine::getIndexExecutor, indexValue.getIndexExecutor());
				blQueryWrapper.lambda().eq(TkIndexValueDiffBaseLine::getIndexExecutorDepart, indexValue.getIndexExecutorDepart());
				blQueryWrapper.lambda().eq(TkIndexValueDiffBaseLine::getIndexDepart, indexValue.getIndexDepart());
				blQueryWrapper.lambda().eq(TkIndexValueDiffBaseLine::getIndexScorer, indexValue.getIndexScorer());
				TkIndexValueDiffBaseLine tkIndexValueDiffBaseLine = tkIndexValueDiffBaseLineMapper.selectOne(blQueryWrapper);

				TkIndexValueDiff tkIndexValueDiff = new TkIndexValueDiff();
				tkIndexValueDiff.setDataType(indexValue.getDataType());
				tkIndexValueDiff.setYear(indexValue.getYear());
				tkIndexValueDiff.setIndexExecutor(indexValue.getIndexExecutor());
				tkIndexValueDiff.setIndexExecutorDepart(indexValue.getIndexExecutorDepart());
				tkIndexValueDiff.setIndexDepart(indexValue.getIndexDepart());
				tkIndexValueDiff.setIndexScorer(indexValue.getIndexScorer());
				tkIndexValueDiff.setIndexCode(indexValue.getIndexCode());
				tkIndexValueDiff.setIndexValueCode(indexValue.getIndexValueCode());
				
				if (null == tkIndexValueDiffBaseLine) {
					tkIndexValueDiff.setMTotal(indexValue.getMTotal());
					tkIndexValueDiff.setM01(indexValue.getM01());
					tkIndexValueDiff.setM02(indexValue.getM02());
					tkIndexValueDiff.setM03(indexValue.getM03());
					tkIndexValueDiff.setM04(indexValue.getM04());
					tkIndexValueDiff.setM05(indexValue.getM05());
					tkIndexValueDiff.setM06(indexValue.getM06());
					tkIndexValueDiff.setM07(indexValue.getM07());
					tkIndexValueDiff.setM08(indexValue.getM08());
					tkIndexValueDiff.setM09(indexValue.getM09());
					tkIndexValueDiff.setM10(indexValue.getM10());
					tkIndexValueDiff.setM11(indexValue.getM11());
					tkIndexValueDiff.setM12(indexValue.getM12());
					tkIndexValueDiff.setQ01(indexValue.getQ01());
					tkIndexValueDiff.setQ02(indexValue.getQ02());
					tkIndexValueDiff.setQ03(indexValue.getQ03());
					tkIndexValueDiff.setQ04(indexValue.getQ04());
					tkIndexValueDiff.setIndexValueStatus("-1");
				} else {
					{
						Double value = indexValue.getMTotal();
						Double blValue = tkIndexValueDiffBaseLine.getMTotal();
						if (value != null && blValue != null) {
							value = value - blValue;
						} else if (value == null && blValue!=null) {
							value = -1 * blValue;
						} else {
							value = 0.0;
						}
						tkIndexValueDiff.setMTotal(value);
					}
					{
						Double value = indexValue.getM01();
						Double blValue = tkIndexValueDiffBaseLine.getM01();
						if (value != null && blValue != null) {
							value = value - blValue;
						} else if (value == null && blValue!=null) {
							value = -1 * blValue;
						} else {
							value = 0.0;
						}
						tkIndexValueDiff.setM01(value);
					}
					{
						Double value = indexValue.getM02();
						Double blValue = tkIndexValueDiffBaseLine.getM02();
						if (value != null && blValue != null) {
							value = value - blValue;
						} else if (value == null && blValue!=null) {
							value = -1 * blValue;
						} else {
							value = 0.0;
						}
						tkIndexValueDiff.setM02(value);
					}
					{
						Double value = indexValue.getM03();
						Double blValue = tkIndexValueDiffBaseLine.getM03();
						if (value != null && blValue != null) {
							value = value - blValue;
						} else if (value == null && blValue!=null) {
							value = -1 * blValue;
						} else {
							value = 0.0;
						}
						tkIndexValueDiff.setM03(value);
					}
					{
						Double value = indexValue.getM04();
						Double blValue = tkIndexValueDiffBaseLine.getM04();
						if (value != null && blValue != null) {
							value = value - blValue;
						} else if (value == null && blValue!=null) {
							value = -1 * blValue;
						} else {
							value = 0.0;
						}
						tkIndexValueDiff.setM04(value);
					}
					{
						Double value = indexValue.getM05();
						Double blValue = tkIndexValueDiffBaseLine.getM05();
						if (value != null && blValue != null) {
							value = value - blValue;
						} else if (value == null && blValue!=null) {
							value = -1 * blValue;
						} else {
							value = 0.0;
						}
						tkIndexValueDiff.setM05(value);
					}
					{
						Double value = indexValue.getM06();
						Double blValue = tkIndexValueDiffBaseLine.getM06();
						if (value != null && blValue != null) {
							value = value - blValue;
						} else if (value == null && blValue!=null) {
							value = -1 * blValue;
						} else {
							value = 0.0;
						}
						tkIndexValueDiff.setM06(value);
					}
					{
						Double value = indexValue.getM07();
						Double blValue = tkIndexValueDiffBaseLine.getM07();
						if (value != null && blValue != null) {
							value = value - blValue;
						} else if (value == null && blValue!=null) {
							value = -1 * blValue;
						} else {
							value = 0.0;
						}
						tkIndexValueDiff.setM07(value);
					}
					{
						Double value = indexValue.getM08();
						Double blValue = tkIndexValueDiffBaseLine.getM08();
						if (value != null && blValue != null) {
							value = value - blValue;
						} else if (value == null && blValue!=null) {
							value = -1 * blValue;
						} else {
							value = 0.0;
						}
						tkIndexValueDiff.setM08(value);
					}
					{
						Double value = indexValue.getM09();
						Double blValue = tkIndexValueDiffBaseLine.getM09();
						if (value != null && blValue != null) {
							value = value - blValue;
						} else if (value == null && blValue!=null) {
							value = -1 * blValue;
						} else {
							value = 0.0;
						}
						tkIndexValueDiff.setM09(value);
					}
					{
						Double value = indexValue.getM10();
						Double blValue = tkIndexValueDiffBaseLine.getM10();
						if (value != null && blValue != null) {
							value = value - blValue;
						} else if (value == null && blValue!=null) {
							value = -1 * blValue;
						} else {
							value = 0.0;
						}
						tkIndexValueDiff.setM10(value);
					}
					{
						Double value = indexValue.getM11();
						Double blValue = tkIndexValueDiffBaseLine.getM11();
						if (value != null && blValue != null) {
							value = value - blValue;
						} else if (value == null && blValue!=null) {
							value = -1 * blValue;
						} else {
							value = 0.0;
						}
						tkIndexValueDiff.setM11(value);
					}
					{
						Double value = indexValue.getM12();
						Double blValue = tkIndexValueDiffBaseLine.getM12();
						if (value != null && blValue != null) {
							value = value - blValue;
						} else if (value == null && blValue!=null) {
							value = -1 * blValue;
						} else {
							value = 0.0;
						}
						tkIndexValueDiff.setM12(value);
					}
					tkIndexValueDiff.setIndexValueStatus("0");
				}
				
				tkIndexValueDiff.setCreateDate(new Date());
				
				tkIndexValueDiffMapper.insert(tkIndexValueDiff);
				
				//如查存在不一致的数据则写入日导对比表中
				Double zeroValue = new Double(0);
				if( !zeroValue.equals(tkIndexValueDiff.getMTotal()) || 
						!zeroValue.equals(tkIndexValueDiff.getM01()) || !zeroValue.equals(tkIndexValueDiff.getM02())
						|| !zeroValue.equals(tkIndexValueDiff.getM03()) || !zeroValue.equals(tkIndexValueDiff.getM04()) 
						|| !zeroValue.equals(tkIndexValueDiff.getM05()) || !zeroValue.equals(tkIndexValueDiff.getM06()) 
						|| !zeroValue.equals(tkIndexValueDiff.getM07()) || !zeroValue.equals(tkIndexValueDiff.getM08()) 
						|| !zeroValue.equals(tkIndexValueDiff.getM09()) || !zeroValue.equals(tkIndexValueDiff.getM10()) 
						|| !zeroValue.equals(tkIndexValueDiff.getM11()) || !zeroValue.equals(tkIndexValueDiff.getM12()) 
						) {
					StringBuilder sbuilder = new StringBuilder();
					sbuilder.append(tkIndexValueDiff.getDataType()).append("@");
					sbuilder.append(tkIndexValueDiff.getIndexValueCode()).append("@");
					sbuilder.append(tkIndexValueDiff.getIndexDepart()).append("@");
					sbuilder.append(tkIndexValueDiff.getIndexScorer());
					String dataId = sbuilder.toString();
					
					SysDataLog sysDataLog = new SysDataLog();
					sysDataLog.setDataTable("tk_index_value");
					sysDataLog.setDataId(dataId);
					//Version为零，表示原始数据
					sysDataLog.setDataVersion("0");
					Map<String, Object> diffValues = new HashMap<String, Object>();

					if (null == tkIndexValueDiffBaseLine) {
						sysDataLog.setDataContent("{}");
					} else {
						diffValues.put("dataType", tkIndexValueDiffBaseLine.getDataType());
						diffValues.put("indexValueCode", tkIndexValueDiffBaseLine.getIndexValueCode());
						diffValues.put("indexDepart", tkIndexValueDiffBaseLine.getIndexDepart());
						diffValues.put("indexScorer", tkIndexValueDiffBaseLine.getIndexScorer());
						diffValues.put("mTotal", tkIndexValueDiffBaseLine.getMTotal());
						diffValues.put("m01", tkIndexValueDiffBaseLine.getM01());
						diffValues.put("m02", tkIndexValueDiffBaseLine.getM02());
						diffValues.put("m03", tkIndexValueDiffBaseLine.getM03());
						diffValues.put("m04", tkIndexValueDiffBaseLine.getM04());
						diffValues.put("m05", tkIndexValueDiffBaseLine.getM05());
						diffValues.put("m06", tkIndexValueDiffBaseLine.getM06());
						diffValues.put("m07", tkIndexValueDiffBaseLine.getM07());
						diffValues.put("m08", tkIndexValueDiffBaseLine.getM08());
						diffValues.put("m09", tkIndexValueDiffBaseLine.getM09());
						diffValues.put("m10", tkIndexValueDiffBaseLine.getM10());
						diffValues.put("m11", tkIndexValueDiffBaseLine.getM11());
						diffValues.put("m12", tkIndexValueDiffBaseLine.getM12());
						sysDataLog.setDataContent(EsJsonUtils.toJsonStr(diffValues));
					}
					sysDataLogMapper.insert(sysDataLog);
					
					sysDataLog = new SysDataLog();
					sysDataLog.setDataTable("tk_index_value");
					sysDataLog.setDataId(dataId);
					//Version为1，表示计算数据
					sysDataLog.setDataVersion("1");
					diffValues = new HashMap<String, Object>();
					diffValues.put("dataType", indexValue.getDataType());
					diffValues.put("indexValueCode", indexValue.getIndexValueCode());
					diffValues.put("indexDepart", indexValue.getIndexDepart());
					diffValues.put("indexScorer", indexValue.getIndexScorer());
					diffValues.put("mTotal", indexValue.getMTotal());
					diffValues.put("m01", indexValue.getM01());
					diffValues.put("m02", indexValue.getM02());
					diffValues.put("m03", indexValue.getM03());
					diffValues.put("m04", indexValue.getM04());
					diffValues.put("m05", indexValue.getM05());
					diffValues.put("m06", indexValue.getM06());
					diffValues.put("m07", indexValue.getM07());
					diffValues.put("m08", indexValue.getM08());
					diffValues.put("m09", indexValue.getM09());
					diffValues.put("m10", indexValue.getM10());
					diffValues.put("m11", indexValue.getM11());
					diffValues.put("m12", indexValue.getM12());
					sysDataLog.setDataContent(EsJsonUtils.toJsonStr(diffValues));
					sysDataLogMapper.insert(sysDataLog);
				}
				
			}
		}
		log.info("end,  process diff, dataType={},indexGroup={}", dataType, indexGroup);

	}

}
